const express = require("express")
const md5 = require("md5")
const model = require("../db/model")
const router = express.Router()
router.post("/registe",async (req,res)=>{
    const { username,password } = req.body
    try{
        await model.create({ username,password: md5(password) })
        res.redirect("http://localhost:5000/login")
    }catch(err){
        res.send("注册失败")
        console.log(err)
    }
})
router.post("/login",async function (req,res){
    const {username,password} = req.body
    try{
        const user = await model.findOne({username, password: md5(password)})
        if(user){
            res.render("manager",user)
        }else{
            res.send('登录失败了,请确认后重新登录')
        }
    }catch(err){
        res.send("登录失败，请稍后再试")
    }
})
module.exports = router